package fr.mairie.parc.page.installation;

import javax.annotation.Resource;

import org.apache.click.control.Form;
import org.apache.click.control.ImageSubmit;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.control.installation.InstallationFields;
import fr.mairie.parc.domain.Installation;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.service.InstallationManager;

public class ViewInstallationPage extends BorderPage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource protected InstallationManager installationManager;
	@Bindable protected Form form = new Form();
	@Bindable protected Integer idInstallation;
	protected transient Installation installation;


	protected InstallationFields fields = new InstallationFields();

	public ViewInstallationPage() {
		fields.setReadonly(true);
		fields.addAll(form);
		form.add(new ImageSubmit("Cancel", "/assets/images/32x32/cancel.png",this, "onCancelClicked"));
	}

	@Override
	public void onInit() {
		super.onInit();
		ApplicationContext ctx = AppContext.getApplicationContext();
    	installationManager = (InstallationManager) ctx.getBean("installationManager");
	}

	@Override
	public void onRender() {
		if (idInstallation < 0) {
			msg = "Erreur, id invalide:" + idInstallation + " pour l'installation";
			setRedirect("error.htm");
			return;
		}
		if (installation == null) {
			installation =  installationManager.getInstallationById(idInstallation);
		}
		if (installation == null) {
			msg = "Erreur, aucune installation trouvé pour cet id :" + idInstallation;
			setRedirect("error.htm");
			return;
		}
		fields.setFieldsValues(installation);
		super.onRender();
	}

	public boolean onCancelClicked() {
        setRedirect(ListInstallationPage.class);
        return false;
    }


}
